/*
 * @Author: yangni 920556271@qq.com
 * @Date: 2024-04-08 17:58:19
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2024-04-11 14:33:26
 * @FilePath: /yt-devops/lib/pages/devices/device_tab/components/child_device_widget.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/gaps.dart';
import 'package:yt_devops/configs.dart';
import 'package:yt_devops/pages/devices/device_list/device_list_model.dart';
import 'package:yt_devops/pages/devices/device_list/device_widget.dart';
import 'package:yt_devops/pages/devices/device_tab/utils/device_util.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/services/device/device_sevices.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/navigate_utils.dart';

class ChildDeviceWidget extends StatefulWidget {
  final int? deviceId;
  final bool isMonitorPage;
  const ChildDeviceWidget(this.deviceId, this.isMonitorPage, {super.key});

  @override
  State<ChildDeviceWidget> createState() => _ChildDeviceWidgetState();
}

class _ChildDeviceWidgetState extends State<ChildDeviceWidget> {
  List<DeviceListModel> deviceList = [];

  List<int> subDevicesIdList = [];

  List<String> subDevicesNameList = [];

  DeviceListModel? deviceTree;

  @override
  void initState() {
    if (widget.deviceId != null) {
      getChildDevice();
    }
    super.initState();
  }

  @override
  void didUpdateWidget(covariant ChildDeviceWidget oldWidget) {
    if (widget.deviceId != null && oldWidget.deviceId != widget.deviceId) {
      getChildDevice();
    }
    super.didUpdateWidget(oldWidget);
  }

  @override
  Widget build(BuildContext context) {
    return deviceList.isEmpty
        ? Gaps.empty
        : ListView.separated(
            shrinkWrap: true,
            physics: const NeverScrollableScrollPhysics(),
            padding: EdgeInsets.only(bottom: 16.h),
            itemBuilder: (context, index) {
              if (index == 0) {
                return DeviceUtil.headTitle(ResKey.childDevice.tr,
                    padding: const EdgeInsets.only(left: 16),
                    height: 56,
                    color: Colors.transparent);
              } else {
                return DeviceWidget(
                  deviceList[index - 1],
                  borderRadius: 0,
                  margin: EdgeInsets.zero,
                  onItemTaped: toDetail,
                  isRealTime: true,
                  titleStyle: TextStyles.f16.mediumStyle,
                );
              }
            },
            separatorBuilder: (context, index) => Container(
              color: Colour.dividerColor,
              height: 1,
              margin: EdgeInsets.only(left: 16.w, right: 16.w),
            ),
            itemCount: deviceList.length + 1,
          );
  }

  getChildDevice() => Configs.httpClient.requestWithLoading(() async {
        var data =
            await DeviceServices().getDevicelist({'deviceId': widget.deviceId});
        var list = data?['list'];
        if (list?[0]?['childDeviceList'] is List) {
          deviceTree = DeviceListModel.fromJson(list[0]);
          var childDeviceList = list?[0]?['childDeviceList'];
          for (var element in childDeviceList) {
            deviceList.add(DeviceListModel.fromJson(element));
            subDevicesIdList.add(element['deviceId']);
            subDevicesNameList.add(element['name'].toString());
          }

          setState(() {});
        }
      }, showLoading: true);

  toDetail(DeviceListModel device) {
    NavigateUtils.toDeviceDetail(device.deviceId!, device.productType!,
        device.name!, device.connectStatus!,
        productId: device.productId,
        subDevicesIdList: subDevicesIdList,
        subDevicesNameList: subDevicesNameList,
        deviceTree: deviceTree);
  }
}
